/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bgu.sonar.util.collections.obs;

import bgu.sonar.util.collections.CategoryMap;
import bgu.sonar.util.collections.HashCategoryMap;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 *
 * @author bennyl
 */
public class ObservableCategoryMap<V> extends HashCategoryMap<V> {

    private ObservableCategoryMap parent = null;
    private String categoryName;

    public ObservableCategoryMap(String categoryName, ObservableCategoryMap<V> parent) {
        this.categoryName = categoryName;
        this.parent = parent;
    }

    public ObservableCategoryMap(String categoryName) {
        this(categoryName, null);
    }

    @Override
    protected HashCategoryMap<V> createSubCategory(String category, CategoryMap<V> parent) {
        return new ObservableCategoryMap<>(category, (ObservableCategoryMap<V>) parent);
    }

    @Override
    protected Map<String, HashCategoryMap<V>> createChildrenMap() {
        return new ObservableMap<>(new HashMap<String, HashCategoryMap<V>>());
    }

    @Override
    protected List<V> createValueList() {
        return new ObservableList<>(new LinkedList<V>());
    }

    @Override
    protected boolean nullifyUnusedCollection() {
        return false;
    }

    @Override
    public ObservableList<V> values() {
        return (ObservableList<V>) super.values();
    }

    public ObservableMap<String, HashCategoryMap<V>> childrenMap() {
        return (ObservableMap<String, HashCategoryMap<V>>) super.getChildrenMap();
    }

    public ObservableCategoryMap getParent() {
        return parent;
    }

    public String getCategoryName() {
        return categoryName;
    }

    public String[] getCategory() {
        LinkedList<String> result = new LinkedList<>();
        ObservableCategoryMap root = this;
        while (root != null) {
            result.addFirst(root.categoryName);
            root = root.parent;
        }

        return result.toArray(new String[result.size()]);
    }
}
